<template>
  <div>
    <v-header></v-header>
    <v-bread>
      <span>商品列表</span>
    </v-bread>
    <div class="goodsList">
      <div class="accessory-result-page">
        <div class="container">
          <div class="filter-nav">
            <span class="sortby">排序:</span>
            <a href="javascript:void(0)" class="default" :class="{'cur':sortFlag}" @click="defaultSort()">默认</a>
            <a href="javascript:void(0)" class="price" :class="{'cur':!sortFlag}" @click="sortGoods()">价格
              <svg class="icon icon-arrow-short">
                <use xlink:href="#icon-arrow-short"></use>
              </svg>
            </a>
            <a href="javascript:void(0)" class="filterby">筛选</a>
          </div>
          <div class="accessory-result">
            <!-- filter -->
            <div class="filter" id="filter">
              <dl class="filter-price">
                <dt>价格区间:</dt>
                <dd><a href="javascript:void(0)" :class="{'cur':priceChecked=='all'}"
                       @click="setPriceFilter('all')">选择价格</a></dd>
                <dd
                  v-for="(item,index) in priceFilter"
                  :key="item.id"
                >
                  <a href="javascript:void(0)" :class="{'cur':priceChecked===index}"
                     @click="setPriceFilter(index)">{{item.startPrice}}-{{item.endPrice}}元</a>
                </dd>
              </dl>
            </div>

            <!-- search result accessories list -->
            <div class="accessory-list-wrap">
              <div class="accessory-list col-4">
                <ul>
                  <li
                    v-for="item in goodsList"
                    :key="item.id"
                  >
                    <div class="pic">
                      <a href="#"><img v-lazy="'/static/img/'+ item.productImage" alt=""></a>
                    </div>
                    <div class="main">
                      <div class="name">{{item.productName}}</div>
                      <div class="price">{{item.salePrice | currency('￥')}}</div>
                      <div class="btn-area">
                        <a href="javascript:;" class="btn btn--m" @click="addCart(item.productId)">加入购物车</a>
                      </div>
                    </div>
                  </li>
                </ul>
              </div>
              <!-- loading more -->
              <div class="view-more-normal"
                   v-infinite-scroll="loadMore"
                   infinite-scroll-disabled="busy"
                   infinite-scroll-distance="20">
                <img src="./loading-svg/loading-spinning-bubbles.svg" v-show="loading">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>

</template>

<script type="text/ecmascript-6">
  import axios from 'axios'
  import header from '../../components/headder/header'
  import bread from '../../components/bread/bread'
  import {currency} from '../../util/currency'

  export default{
    components: {
      'v-header': header,
      'v-bread': bread
    },
    data () {
      return {
        goodsList: [],
        priceFilter: [
          {
            startPrice: '0.00',
            endPrice: '100.00'
          },
          {
            startPrice: '100.00',
            endPrice: '500.00'
          },
          {
            startPrice: '500.00',
            endPrice: '1000.00'
          },
          {
            startPrice: '1000.00',
            endPrice: '2000.00'
          },
          {
            startPrice: '2000.00',
            endPrice: '3000.00'
          },
          {
            startPrice: '3000.00',
            endPrice: '6000.00'
          }
        ],
        sortFlag: true, // 排序
        busy: true,
        page: 1,
        pageSize: 8,
        priceChecked: 'all',
        loading: false,
        mdShow: false,
        mdShowCart: false
      }
    },
    mounted () {
      this.getGoodsListData();
    },
    filters: {
      currency: currency
    },
    methods: {
      getGoodsListData (flag) {
        var params = {
          page: this.page,
          pageSize: this.pageSize,
          sort: this.sortFlag ? 1 : -1,
          priceLevel: this.priceChecked
        };
        this.loading = true;
        axios.get('/goods', {
          params: params
        }).then((result) => {
          this.loading = false;
          if (flag) {
            // 多次加载数据
            this.goodsList = this.goodsList.concat(result.data.result);
            if (result.data.result.length === 0) {
              this.busy = true;
            } else {
              this.busy = false;
            }
          } else {
            // 第一次加载数据
            this.goodsList = result.data.result;
            // 当第一次加载数据完之后，把这个滚动到底部的函数触发打开
            this.busy = false;
          }
        })
      },
      loadMore: function () {
        this.busy = true;
        // 多次加载数据
        setTimeout(() => {
          this.page++;
          this.getGoodsListData(true);
        }, 1000);
      },
      defaultSort () {
        this.sortFlag = true;
        this.page = 1;
        this.getGoodsListData();
      },
      sortGoods () {
        this.sortFlag = false;
        this.page = 1;
        this.getGoodsListData();
      },
      setPriceFilter (index) {
        this.priceChecked = index;
        this.page = 1;
        this.getGoodsListData();
      },
      addCart (productId) {
        axios.post('/goods/addCart', {
          productId: productId
        }).then((response) => {
          var res = response.data;
          console.log(res);
          if (res.status === '0') {
            this.mdShowCart = true;
            this.$store.commit('updateCartCount', 1);
          } else {
            this.mdShow = true;
          }
        });
      }
    }
  }
</script>

<style rel="stylesheet">
  .accessory-list > ul:after {
    visibility: hidden;
    display: block;
    content: " ";
    clear: both;
  }

  .accessory-list > ul > li {
    float: left;
    width: 23.80952%;
    margin-right: 1.5873%;
    margin-bottom: 1.5873%;
    background: #fff;
    border: 2px solid #e9e9e9;
    -webkit-transition: all .5s ease-out;
    transition: all .5s ease-out;
  }

  .accessory-list > ul > li:hover {
    border-color: #ee7a23;
    -webkit-transform: translateY(-5px);
    -ms-transform: translateY(-5px);
    transform: translateY(-5px);
    -webkit-box-shadow: 0 0 10px #999;
    box-shadow: 0 0 10px #999;
    -webkit-transition: all .5s ease-out;
    transition: all .5s ease-out;
  }

  .accessory-list.col-4 > ul > li {
    width: 23.80952%;
    margin-right: 1.5873%;
    margin-bottom: 1.5873%;
  }

  .accessory-list.col-4 > ul > li:nth-child(4n) {
    margin-right: 0;
  }

  .accessory-list.col-5 > ul > li {
    width: 18.73016%;
    margin-right: 1.5873%;
    margin-bottom: 1.5873%;
  }

  .accessory-list.col-5 > ul > li:nth-child(5n) {
    margin-right: 0;
  }

  .accessory-list .pic {
    overflow: hidden;
  }

  .accessory-list .pic a {
    display: block;
    width: 100%;
    height: 100%;
  }

  .accessory-list .pic img {
    width: 100%;
  }

  .accessory-list .main {
    padding: 10px 10px 10px 10px;
  }

  .accessory-list .main .name {
    height: 2em;
    line-height: 1.25em;
    padding-bottom: 10px;
    font-family: "moderat", sans-serif;
    font-weight: bold;
    overflow: hidden;
  }

  .accessory-list .main .price {
    float: left;
    line-height: 30px;
    color: #d1434a;
    font-size: 1.25em;
  }

  .accessory-list .main .quantity {
    float: right;
  }

  .accessory-list .main .btn-area {
    clear: both;
    padding-top: 10px;
  }

  .accessory-list .main .btn-area .btn {
    width: 100%;
  }

  @media only screen and (max-width: 991px) {
    .accessory-list.col-4 > ul > li {
      width: 32.27513%;
      margin-right: 1.5873%;
      margin-bottom: 1.5873%;
    }

    .accessory-list.col-4 > ul > li:nth-child(4n) {
      margin-right: 1.5873%;
    }

    .accessory-list.col-4 > ul > li:nth-child(3n) {
      margin-right: 0;
    }

    .accessory-list.col-5 > ul > li {
      width: 23.80952%;
      margin-right: 1.5873%;
      margin-bottom: 1.5873%;
    }

    .accessory-list.col-5 > ul > li:nth-child(5n) {
      margin-right: 20px;
    }

    .accessory-list.col-5 > ul > li:nth-child(4n) {
      margin-right: 0;
    }

    .accessory-list .main .name {
      font-size: 1.3rem;
    }

    .accessory-list .main .btn-area .btn {
      font-size: 1.2rem;
    }
  }

  @media only screen and (max-width: 767px) {
    .accessory-list {
      font-size: 1.2rem;
    }

    .accessory-list.col-4 > ul > li, .accessory-list.col-5 > ul > li {
      width: 100%;
      margin-bottom: 10px;
      padding: 10px;
      border: none;
      border-top: 1px solid #e9e9e9;
      border-bottom: 1px solid #e9e9e9;
    }

    .accessory-list.col-4 > ul > li:hover, .accessory-list.col-5 > ul > li:hover {
      border-color: none;
      -webkit-transform: none;
      -ms-transform: none;
      transform: none;
      -webkit-box-shadow: none;
      box-shadow: none;
      -webkit-transition: none .5s ease-out;
      transition: none .5s ease-out;
    }

    .accessory-list .pic {
      float: left;
      width: 110px;
      height: 72px;
      border: 1px solid #e9e9e9;
    }

    .accessory-list .pic a {
      display: block;
      width: 100%;
      height: 100%;
      padding-bottom: 0;
    }

    .accessory-list .main {
      padding: 0 0 0 90px;
    }

    .accessory-list .main:after {
      visibility: hidden;
      display: block;
      content: " ";
      clear: both;
    }

    .accessory-list .main .price {
      float: none;
    }

    .accessory-list .main .quantity {
      float: left;
    }

    .accessory-list .main .name {
      height: auto;
      min-height: 30px;
    }

    .accessory-list .main .btn-area {
      padding: 0;
      clear: none;
      float: right;
    }

    .accessory-list .main .btn-area .btn {
      height: 30px;
      line-height: 30px;
      padding-left: .8em;
      padding-right: .8em;
      font-size: 1rem;
      letter-spacing: .1em;
    }
  }

  .filter {
    width: 230px;
    padding: 0 20px 0 20px;
    color: #605F5F;
    -webkit-transition: right .5s ease-out;
    transition: right .5s ease-out;
  }

  .filter dl {
    min-height: 180px;
    margin-bottom: 50px;
  }

  .filter dt {
    margin-bottom: 30px;
    font-family: "moderat", sans-serif;
    letter-spacing: .25em;
    text-transform: uppercase;
    font-weight: bold;
  }

  .filter dd {
    line-height: 1.2em;
    margin: 20px 0;
  }

  .filter dd a {
    display: block;
    padding: 5px 0;
    -webkit-transition: padding-left .3s ease-out;
    transition: padding-left .3s ease-out;
  }

  .filter dd a:hover, .filter dd .cur {
    color: #ee7a23;
  }

  .filter .filter-category ul {
    display: none;
    padding-left: 20px;
  }

  .filter .filter-category li {
    line-height: 1.2em;
    margin: 10px 0;
  }

  .filter .filter-category ul > li > ul {
    margin-bottom: 20px;
  }

  .filter .filter-category ul > li > ul .icon-arrow-down {
    display: none;
  }

  .filter .filter-category a:hover .icon-arrow-down {
    fill: #ee7a23;
    -webkit-transition: -webkit-transform .3s ease-out;
    transition: -webkit-transform .3s ease-out;
    transition: transform .3s ease-out;
    transition: transform .3s ease-out, -webkit-transform .3s ease-out;
  }

  .filter .filter-category .icon-arrow-down {
    width: 11px;
    height: 11px;
    fill: #605F5F;
    vertical-align: middle;
    margin-left: 3px;
    -webkit-transition: -webkit-transform .3s ease-out;
    transition: -webkit-transform .3s ease-out;
    transition: transform .3s ease-out;
    transition: transform .3s ease-out, -webkit-transform .3s ease-out;
  }

  .filter .filter-category .open ~ ul {
    display: block;
  }

  .filter .filter-category .open .icon-arrow-down {
    -webkit-transform: rotate(180deg);
    -ms-transform: rotate(180deg);
    transform: rotate(180deg);
  }

  .filter .filter-price dd a:hover, .filter .filter-price dd .cur {
    -webkit-transition: padding-left .3s ease-out;
    transition: padding-left .3s ease-out;
    border-left: 2px solid #ee7a23;
    padding-left: 15px;
  }

  .filter .filter-price dd .cur {
    font-weight: bold;
  }

  .filter.filterby-show {
    right: 0;
    -webkit-transition: right .5s ease-out;
    transition: right .5s ease-out;
  }

  @media only screen and (max-width: 767px) {
    .filter {
      position: fixed;
      right: -230px;
      top: 0;
      z-index: 201;
      height: 100%;
      background: #fff;
      padding: 0;
      margin: 0;
      overflow: auto;
    }

    .filter dl {
      min-height: 0;
      margin: 0;
    }

    .filter dt {
      background: #f0f0f0;
      height: 55px;
      line-height: 55px;
      margin: 0;
      padding-left: 20px;
    }

    .filter dd {
      margin: 0;
    }

    .filter dd a {
      padding: 12px 10px 12px 15px;
      border-bottom: 1px solid #e9e9e9;
    }

    .filter dd li {
      margin: 0;
    }

    .filter .filter-category ul {
      padding-left: 0;
    }

    .filter .filter-category ul li > a {
      padding-left: 30px;
    }

    .filter .filter-category ul li > ul a {
      padding-left: 45px;
    }

    .filter .filter-category .icon-arrow-down {
      float: right;
    }
  }

  /** search resule **/
  /** 搜索结果页 **/
  .search-result-wrap {
    background: #f0f0f0;
  }

  .search-result-wrap .search-result-num {
    padding: 10px 0;
    line-height: 25px;
  }

  .search-result-wrap .search-result-num strong {
    color: #d1434a;
  }

  .accessory-result {
    display: -webkit-box;
    display: -webkit-flex;
    display: -ms-flexbox;
    display: flex;
  }

  .accessory-result .filter {
    margin-right: 25px;
  }

  .accessory-result .accessory-list-wrap {
    -webkit-box-flex: 1;
    -webkit-flex: 1;
    -ms-flex: 1;
    flex: 1;
  }

  .filter-nav {
    height: 55px;
    line-height: 55px;
    margin: 60px 0 30px 0;
    padding: 0 20px;
    background: #fff;
    text-align: right;
    overflow: hidden;
  }

  .filter-nav a {
    margin: 0 10px;
  }

  .filter-nav a.cur, .filter-nav a:hover {
    color: #ee7a23;
  }

  .filter-nav .sort-up .icon-arrow-short {
    -webkit-transform: rotate(180deg);
    -ms-transform: rotate(180deg);
    transform: rotate(180deg);
  }

  .filter-nav .filterby {
    display: none;
    letter-spacing: .25em;
    text-transform: uppercase;
    font-size: 12px;
  }

  @media only screen and (max-width: 767px) {
    .search-result-wrap {
      background: none;
      padding: 0 10px;
    }

    .filter-nav {
      margin: 0;
      padding: 0 10px;
      background: #f0f0f0;
      border-top: 1px solid #e9e9e9;
      text-align: left;
    }

    .filter-nav:after {
      visibility: hidden;
      display: block;
      content: " ";
      clear: both;
    }

    .filter-nav a {
      float: left;
    }

    .filter-nav .filterby {
      display: block;
      float: right;
    }

    .filter-nav .sortby {
      display: none;
    }

    .accessory-result .filter {
      margin: 0;
    }
  }

  .icon-arrow-short {
    width: 11px;
    height: 11px;
  }

  .page-title-accessory {
    display: none;
  }

  .accessory-title:after {
    visibility: hidden;
    display: block;
    content: " ";
    clear: both;
  }

  .accessory-title h2 {
    float: left;
  }

  .accessory-title .accessory-more-link {
    float: right;
    line-height: 30px;
  }

  .accessory-title .accessory-more-link .icon-arrow-short {
    margin-left: 15px;
    -webkit-transform: rotate(-90deg);
    -ms-transform: rotate(-90deg);
    transform: rotate(-90deg);
  }

  .accessory-title .accessory-more-link:hover {
    color: #ee7a23;
  }

  .accessory-title .accessory-more-link:hover .icon-arrow-short {
    fill: #ee7a23;
  }

  .accessory-classify {
    position: relative;
  }

  .accessory-result-page .view-more-normal {
    height: 120px;
    margin-top: 40px;
  }

  .accessory-result-page .view-more-normal .view-more-btn {
    top: 0;
    padding-top: 40px;
  }

  @media only screen and (max-width: 767px) {
    .page-title-accessory {
      display: block;
    }

    .accessory-classify {
      padding-bottom: 55px;
    }

    .accessory-title {
      padding-left: 10px;
      padding-right: 10px;
      border: none;
    }

    .accessory-title .accessory-more-link {
      position: absolute;
      bottom: 10px;
      left: 0;
      width: 100%;
      height: 45px;
      line-height: 45px;
      background: #fff;
      border-top: 1px solid #e9e9e9;
      border-bottom: 1px solid #e9e9e9;
    }

    .accessory-result-page {
      font-size: 1.2rem;
    }

    .accessory-result-page .btn-wrap {
      margin: 0;
    }

    .accessory-result-page .btn-wrap .btn {
      /*line-height: 1.2em;*/
      white-space: normal;
    }

    .accessory-result-page .view-more-normal {
      height: 60px;
      margin-top: 10px;
    }

    .accessory-result-page .view-more-normal .view-more-btn {
      top: 0;
      padding-top: 20px;
    }
  }
</style>
